/**
 * 特定员工搜索
 */
import React from 'react';
import { Select } from 'antd';
import { useRequest } from '@/hooks';
import { queryEmployee } from '../../service';

const Employee = React.forwardRef(({ value = {}, onChange, params = {}, ...rest }, ref) => {
    const { data = {} } = useRequest(() => queryEmployee({ id: params.id }));

    const handleChange = (_value, item = {}) => {
        const { props = {} } = item;
        onChange({ value: props.value, label: props.children });
    };

    const { data: sourceData = [] } = data;

    const options = React.useMemo(() => {
        if (!sourceData.length) {
            return null;
        }
        return sourceData.map(item => <Select.Option value={item.employeeCode}>{item.employeeName}</Select.Option>);
    }, [sourceData]);

    return (
        <Select
            showSearch
            value={value.value}
            allowClear
            onChange={handleChange}
            placeholder="请选择"
            filterOption={(input, option) => option.props.children.indexOf(input) >= 0}
            notFoundContent={null}
            ref={ref}
            {...rest}
        >
            {options}
        </Select>
    );
});

export default Employee;
